﻿using System;
using System.ServiceProcess;
using System.Threading;
using AccessControl.SecurityMonitoring.ACServiceAgent;
using AccessControl.SecurityMonitoring.Properties;
using AccessControl.Utility.Tools;
using DevExpress.XtraEditors;


namespace AccessControl.SecurityMonitoring.Form
{
    public partial class WindowsServiceManager : XtraForm
    {

        #region SingleTone Implementation

        private static WindowsServiceManager _windowsServiceManager;

        private WindowsServiceManager()
        {
            InitializeComponent();
        }

        public static WindowsServiceManager GetInstance()
        {
            if ((_windowsServiceManager == null) || (_windowsServiceManager.IsDisposed))
                _windowsServiceManager = new WindowsServiceManager();
            return _windowsServiceManager;
        }

        #endregion

        #region Event Handler
        
        private void WindowsServiceManager_Load(object sender, EventArgs e)
        {
            GetServiceStatus();
        }

        private void simpleButtonStart_Click(object sender, EventArgs e)
        {
            try
            {
                
            var fingerPrintEngineServerClient = new ACServiceAgent.ACServiceAgentClient("TcpBindingACAgent");
            fingerPrintEngineServerClient.ClientCredentials.Windows.ClientCredential.Domain = Settings.Default.Domain;
            fingerPrintEngineServerClient.ClientCredentials.Windows.ClientCredential.UserName = Settings.Default.Username;
            fingerPrintEngineServerClient.ClientCredentials.Windows.ClientCredential.Password = Settings.Default.Password;

            fingerPrintEngineServerClient.Open();
            fingerPrintEngineServerClient.StartService();
            fingerPrintEngineServerClient.Close();
                Monitoring.showRetryMessage = true;
            GetServiceStatus();
            }
            catch (Exception ex)
            {

                WriteLog(ex.Message,"Start Service",eventLevelConstants.Error);
            }
        }

        private void simpleButtonStop_Click(object sender, EventArgs e)
        {
            try
            {
                var fingerPrintEngineServerClient = new ACServiceAgent.ACServiceAgentClient("TcpBindingACAgent");
            fingerPrintEngineServerClient.ClientCredentials.Windows.ClientCredential.Domain = Settings.Default.Domain;
            fingerPrintEngineServerClient.ClientCredentials.Windows.ClientCredential.UserName = Settings.Default.Username;
            fingerPrintEngineServerClient.ClientCredentials.Windows.ClientCredential.Password = Settings.Default.Password;

            fingerPrintEngineServerClient.Open();
            fingerPrintEngineServerClient.StopService();
            fingerPrintEngineServerClient.Close();
                Monitoring.showRetryMessage = false;
            GetServiceStatus();
            }
            catch (Exception ex)
            {

                WriteLog(ex.Message,"Stop Service",eventLevelConstants.Error);
            }
        }

        private void simpleButtonRestart_Click(object sender, EventArgs e)
        {
            try
            {          
            var fingerPrintEngineServerClient = new ACServiceAgent.ACServiceAgentClient("TcpBindingACAgent");
            fingerPrintEngineServerClient.ClientCredentials.Windows.ClientCredential.Domain = Settings.Default.Domain;
            fingerPrintEngineServerClient.ClientCredentials.Windows.ClientCredential.UserName = Settings.Default.Username;
            fingerPrintEngineServerClient.ClientCredentials.Windows.ClientCredential.Password = Settings.Default.Password;

            fingerPrintEngineServerClient.Open();
            fingerPrintEngineServerClient.RestartService();
            fingerPrintEngineServerClient.Close();
            Thread.Sleep(5000);
            GetServiceStatus();
            }
            catch (Exception ex)
            {

                WriteLog(ex.Message,"Restart Service",eventLevelConstants.Error);
            }
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            GetServiceStatus();
        }

        #endregion

        #region Private Method
        
        private static void WriteLog(String message, String callerMethod, eventLevelConstants level)
        {
            LogFile.WriteLog(LogFile.DefaultLogfileLocation + "Security Monitoring\\" + DateTime.Now.Date.ToString("yyyyMMdd") + " " + "WindowsServiceManager " + callerMethod + ".txt", level, message);
        }

        private void GetServiceStatus()
        {
            try
            {
                var fingerPrintEngineServerClient = new ACServiceAgent.ACServiceAgentClient("TcpBindingACAgent");
                fingerPrintEngineServerClient.ClientCredentials.Windows.ClientCredential.Domain = Settings.Default.Domain;
                fingerPrintEngineServerClient.ClientCredentials.Windows.ClientCredential.UserName = Settings.Default.Username;
                fingerPrintEngineServerClient.ClientCredentials.Windows.ClientCredential.Password = Settings.Default.Password;

                fingerPrintEngineServerClient.Open();
                var status = fingerPrintEngineServerClient.GetServiceStatus();
                fingerPrintEngineServerClient.Close();
                switch (status)
                {
                    case ServiceControllerStatus.Running:
                        pictureEdit.Image = Resources.Green;
                        simpleButtonStart.Enabled = false;
                        simpleButtonRestart.Enabled = true;
                        simpleButtonStop.Enabled = true;
                        break;
                    case ServiceControllerStatus.Stopped:
                        pictureEdit.Image = Resources.Red;
                        simpleButtonStart.Enabled = true;
                        simpleButtonRestart.Enabled = false;
                        simpleButtonStop.Enabled = false;
                        break;
                    default:
                        pictureEdit.Image = Resources.Yellow;
                        simpleButtonStart.Enabled = false;
                        simpleButtonRestart.Enabled = true;
                        simpleButtonStop.Enabled = false;
                        break;
                }
            }
            catch (Exception ex)
            {

                WriteLog(ex.Message, "GetServiceStatus", eventLevelConstants.Error);
            }

        }

        #endregion  

    }
}